package com.wyg.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.wyg.dao.UserDao;
import com.wyg.pojo.Question;
import com.wyg.pojo.Test;
import com.wyg.pojo.User;

public class UserDaoImpl implements UserDao {
	
	private SessionFactory sf;

	public SessionFactory getSf() {
		return sf;
	}

	public void setSf(SessionFactory sf) {
		this.sf = sf;
	}

	@Override
	public User login(String phone, String password) {
		Session session = sf.getCurrentSession();
		Transaction trans = session.beginTransaction();
		//查询满足条件的数据
		Query query = session.createQuery("from User u where u.phoneNumber=:phone and u.pwd=:password");
		query.setParameter("phone", phone);
		query.setParameter("password", password);
		User user = (User) query.uniqueResult();
		trans.commit();
		return user;
	}

	@Override
	public void regist(User user) {
		Session session = sf.getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		session.save(user);
		trans.commit();

	}

	@Override
	public List findAllUser() {
		Session session = sf.getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		Query query = session.createQuery("from User");
		List list = query.list();
		trans.commit();
		return list;
	}

	@Override
	public void changeChiceQuestion(int id, String title, String optionA, String optionB, String optionC,
			String optionD, String answer) {
		Session session = sf.getCurrentSession();
		Transaction t = session.beginTransaction();
		
		Question q = (Question) session.get(Question.class, id);
		q.setTitle(title);
		q.setOptionA(optionA);
		q.setOptionB(optionB);
		q.setOptionC(optionC);
		q.setOptionD(optionD);
		q.setAnswer(answer);
		
		t.commit();
	}

	@Override
	public Question showOneQuestion(int id) {
		Session session = sf.getCurrentSession();
		Transaction t = session.beginTransaction();
		
		Question q = (Question) session.get(Question.class, id);
		t.commit();
		return q;
	}
	//通过id 得到User
	@Override
	public User getUser(int id) {
		Session session = sf.getCurrentSession();
		Transaction t = session.beginTransaction();
		
		User u = (User) session.get(User.class, id);
		t.commit();
		return u;
	}
	//设置头像路径
	@Override
	public void setPicPath(int id, String path) {
		Session session = sf.getCurrentSession();
		Transaction t = session.beginTransaction();
		
		User u = (User) session.get(User.class, id);
		u.setPicPath(path);
		t.commit();
	}
	//激活
	@Override
	public void doActive(int id) {
		Session session = sf.getCurrentSession();
		Transaction t = session.beginTransaction();
		
		User u = (User) session.get(User.class, id);
		u.setStatus("已激活");
		t.commit();
	}
	//考试注册
	@Override
	public void registTest(Test test) {
		Session session = sf.getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		session.save(test);
		trans.commit();		
	}
	//通过电话找到对应的考生信息
	@Override
	public Test getTestByPhone(String phone) {
		Session session = sf.getCurrentSession();
		Transaction trans = session.beginTransaction();
		
		String hql = "from Test t where t.phoneNumber=:phone";
		Query query = session.createQuery(hql);
		query.setParameter("phone", phone);
		Test test = (Test) query.uniqueResult();
		trans.commit();
		return test;
	}

	
}
